﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Retete.ViewModels;
using System.Globalization;
using Retete.Models;
using System.IO;
using System.Configuration;

namespace Retete.Helpers
{
    public static class Rapoarte
    {
        public static string GenereazaRaportBonConsum(DateTime data, string categorie, List<StocViewModel> produse)
        {
            StringBuilder builder = new StringBuilder();
            string headerStyle = "border-left-width: 1px; border-color: #999999; font-weight: bold; border-style: solid; border-width: 1px; border-collapse: collapse; ";
            string rowStyle = "border-style: solid; border-color: #999999; border-left-width: 1px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 0px; border-collapse: collapse; ";
            string footerStyle = "border-style: solid; border-color: #999999; border-top-width: 1px; border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-collapse: collapse; ";

            builder.AppendLine("<html>");
            builder.AppendLine("  <head>");
            builder.AppendLine("      <title>Inventar</title>");
            builder.AppendLine("      <script type='text/javascript'>window.print()</script>");
            builder.AppendLine("  </head>");
            builder.AppendLine("  <body style='font-family: Calibri; font-size: 11pt; color: #999999'>");
            builder.AppendLine("  <h1 style='font-size: 16pt'>Bonul de consum nr. ........... din data de " + data.ToString("dd/MM/yyyy") + "</h1>");
            
            // Bucatarie
            builder.AppendLine("  <b>Categorie: </b>Bucatarie");
            builder.AppendLine("  <table cellpadding='2px' cellspacing='0px'>");
            builder.AppendLine("      <tr style='text-align: center'>");
            builder.AppendLine("          <td style='width: 80px; " + headerStyle + "'>Cod</td>");
            builder.AppendLine("          <td style='width: 400px; " + headerStyle + "'>Produs</td>");
            builder.AppendLine("          <td style='width: 60px; " + headerStyle + "'>UM</td>");
            builder.AppendLine("          <td style='width: 60px; " + headerStyle + "'>Cant.</td>");
            builder.AppendLine("          <td style='width: 80px; " + headerStyle + "'>Pret achizitie</td>");
            builder.AppendLine("          <td style='width: 100px; " + headerStyle + "'>Valoare</td>");
            builder.AppendLine("      </tr>");

            if (produse != null)
            {
                foreach (StocViewModel element in produse.Where(element => element.Categorie != "Bauturi"))
                {
                    builder.AppendLine("      <tr>");
                    builder.AppendLine("          <td style='" + rowStyle + "'>" + element.Cod + "</td>");
                    builder.AppendLine("          <td style='" + rowStyle + "'>" + element.Nume + "</td>");
                    builder.AppendLine("          <td style='text-align: center; " + rowStyle + "'>" + element.UnitateMasura + "</td>");
                    builder.AppendLine("          <td style='" + rowStyle + "'>" + element.Cantitate.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("          <td style='" + rowStyle + "'>" + element.PretCumparare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("          <td style='" + rowStyle + "'>" + element.ValoareCumparare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("      </tr>");
                }

                builder.AppendLine("    <tr>");
                builder.AppendLine("        <td style='" + footerStyle + "'></td>");
                builder.AppendLine("        <td style='" + footerStyle + "'>Total:</td>");
                builder.AppendLine("        <td style='" + footerStyle + "'></td>");
                builder.AppendLine("        <td style='" + footerStyle + "'></td>");
                builder.AppendLine("        <td style='" + footerStyle + "'></td>");
                builder.AppendLine("        <td style='" + footerStyle + "'>" + produse.Where(element => element.Categorie != "Bauturi").Sum(element => element.ValoareCumparare).ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                builder.AppendLine("    </tr>");
            }

            builder.AppendLine("  </table>");
            builder.AppendLine("  <br />");

            // Bauturi
            builder.AppendLine("  <b>Categorie: </b>Bauturi");
            builder.AppendLine("  <table cellpadding='2px' cellspacing='0px'>");
            builder.AppendLine("      <tr style='text-align: center'>");
            builder.AppendLine("          <td style='width: 80px; " + headerStyle + "'>Cod</td>");
            builder.AppendLine("          <td style='width: 400px; " + headerStyle + "'>Produs</td>");
            builder.AppendLine("          <td style='width: 60px; " + headerStyle + "'>UM</td>");
            builder.AppendLine("          <td style='width: 60px; " + headerStyle + "'>Cant.</td>");
            builder.AppendLine("          <td style='width: 80px; " + headerStyle + "'>Pret achizitie</td>");
            builder.AppendLine("          <td style='width: 100px; " + headerStyle + "'>Valoare</td>");
            builder.AppendLine("      </tr>");

            if (produse != null)
            {
                foreach (StocViewModel element in produse.Where(element => element.Categorie == "Bauturi"))
                {
                    builder.AppendLine("      <tr>");
                    builder.AppendLine("          <td style='" + rowStyle + "'>" + element.Cod + "</td>");
                    builder.AppendLine("          <td style='" + rowStyle + "'>" + element.Nume + "</td>");
                    builder.AppendLine("          <td style='text-align: center; " + rowStyle + "'>" + element.UnitateMasura + "</td>");
                    builder.AppendLine("          <td style='" + rowStyle + "'>" + element.Cantitate.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("          <td style='" + rowStyle + "'>" + element.PretCumparare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("          <td style='" + rowStyle + "'>" + element.ValoareCumparare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("      </tr>");
                }

                builder.AppendLine("    <tr>");
                builder.AppendLine("        <td style='" + footerStyle + "'></td>");
                builder.AppendLine("        <td style='" + footerStyle + "'>Total:</td>");
                builder.AppendLine("        <td style='" + footerStyle + "'></td>");
                builder.AppendLine("        <td style='" + footerStyle + "'></td>");
                builder.AppendLine("        <td style='" + footerStyle + "'></td>");
                builder.AppendLine("        <td style='" + footerStyle + "'>" + produse.Where(element => element.Categorie == "Bauturi").Sum(element => element.ValoareCumparare).ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                builder.AppendLine("    </tr>");
            }

            builder.AppendLine("  </table>");
            builder.AppendLine("  <br />");

            // Sfarsit
            builder.AppendLine("      <br />");
            builder.AppendLine("      <b>Gestionar, </b><br />");
            builder.AppendLine("      <b>Farcas Ana-Doina, </b><br />");
            builder.AppendLine("  </body>");
            builder.AppendLine("</html>");

            return builder.ToString();
        }
        public static string GenereazaRaportProductieNou(DateTime data, string categorie, List<StocViewModel> vanzari, List<StocViewModel> vanzariRetete)
        {
            StringBuilder builder = new StringBuilder();
            string headerStyle = "border-left-width: 1px; border-color: #999999; font-weight: bold; border-style: solid; border-width: 1px; border-collapse: collapse; ";
            string rowStyle = "border-style: solid; border-color: #000000; border-left-width: 1px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 0px; border-collapse: collapse; ";
            string footerStyle = "border-style: solid; border-color: #000000; border-top-width: 1px; border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-collapse: collapse; ";

            builder.AppendLine("<html>");
            builder.AppendLine("  <head>");
            builder.AppendLine("      <title>Inventar</title>");
            builder.AppendLine("      <script type='text/javascript'>window.print()</script>");
            builder.AppendLine("  </head>");
            builder.AppendLine("  <body style='font-family: Calibri; font-size: 11pt; color: #999999'>");
            builder.AppendLine("  <h1 style='font-size: 16pt'>Raportul de productie nr. ........... din data de " + data.ToString("dd/MM/yyyy") + "</h1>");
            builder.AppendLine("  <b>Categorie: </b>" + (string.IsNullOrEmpty(categorie) == true ? "Toate" : categorie));

            // lista produselor vandute
            builder.AppendLine("      <table cellpadding='1px' cellspacing='0px'>");
            builder.AppendLine("          <tr style='text-align: center'>");
            builder.AppendLine("              <td style='width: 70px; " + headerStyle + "'></td>");
            builder.AppendLine("              <td style='width: 400px; " + headerStyle + "'>Materie prima</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>UM</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Cantitate</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Pret unitar</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Valoare</td>");
            builder.AppendLine("          </tr>");

            if (vanzari != null)
            {
                foreach (StocViewModel element in vanzari)
                {
                    builder.AppendLine("          <tr>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + (vanzari.IndexOf(element) + 1).ToString() + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Nume + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "; text-align: center;'>" + element.UnitateMasura + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Cantitate.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.PretCumparare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.ValoareCumparare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("          </tr>");
                }

                builder.AppendLine("        <tr>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'>Total:</td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'>" + vanzari.Sum(filtru => filtru.ValoareCumparare).ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                builder.AppendLine("        </tr>");
            }

            builder.AppendLine("      </table>");
            builder.AppendLine("      <br />");

            // lista retetelor vandute
            builder.AppendLine("  <h1 style='font-size: 12pt'>S-au obtinut urmatoarele:</h1>");
            builder.AppendLine("      <table cellpadding='2px' cellspacing='0px'>");
            builder.AppendLine("          <tr style='text-align: center'>");
            builder.AppendLine("              <td style='width: 70px; " + headerStyle + "'></td>");
            builder.AppendLine("              <td style='width: 400px; " + headerStyle + "'>Produs</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>UM</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Cantitate</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Pret unitar</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Valoare</td>");
            builder.AppendLine("          </tr>");

            if (vanzari != null)
            {
                for (int i = 0; i < vanzariRetete.Count; i++)
                {
                    builder.AppendLine("          <tr>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + (i + 1).ToString() + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + vanzariRetete[i].Nume + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "; text-align: center;'>buc</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + vanzariRetete[i].Cantitate.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + vanzariRetete[i].PretCumparare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + vanzariRetete[i].ValoareCumparare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("          </tr>");
                }

                builder.AppendLine("        <tr>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'>Total:</td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'>" + vanzariRetete.Sum(filtru => filtru.ValoareCumparare).ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                builder.AppendLine("        </tr>");
            }

            builder.AppendLine("      </table>");
            builder.AppendLine("      <br />");

            builder.AppendLine("      <b>Gestionar, </b><br />");
            builder.AppendLine("      <b>Farcas Ana-Doina, </b><br />");
            builder.AppendLine("  </body>");
            builder.AppendLine("</html>");

            return builder.ToString();
        }
        public static string GenereazaRaportProductie(DateTime data, List<RaportProductieElementViewModel> elemente)
        {
            StringBuilder builder = new StringBuilder();
            string headerStyle = "border-left-width: 1px; border-color: #999999; font-weight: bold; border-style: solid; border-width: 1px; border-collapse: collapse; ";
            string rowStyle = "border-style: solid; border-color: #999999; border-left-width: 1px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 0px; border-collapse: collapse; ";
            string footerStyle = "border-style: solid; border-color: #999999; border-top-width: 1px; border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-collapse: collapse; ";

            builder.AppendLine("<html>");
            builder.AppendLine("  <head>");
            builder.AppendLine("      <title>Inventar</title>");
            builder.AppendLine("      <script type='text/javascript'>window.print()</script>");
            builder.AppendLine("  </head>");
            builder.AppendLine("  <body style='font-family: Calibri; font-size: 11pt; color: #999999'>");
            builder.AppendLine("  <h1 style='font-size: 16pt'>Raportul de productie nr. ........... din data de " + data.ToString("dd/MM/yyyy") + "</h1>");
            //builder.AppendLine("  <b>Categorie: </b>" + (string.IsNullOrEmpty(categorie) == true ? "Toate" : categorie));

            builder.AppendLine("      <table cellpadding='2px' cellspacing='0px'>");
            builder.AppendLine("          <tr style='text-align: center'>");
            builder.AppendLine("              <td style='width: 70px; " + headerStyle + "'>Cod</td>");
            builder.AppendLine("              <td style='width: 400px; " + headerStyle + "'>Produs</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Stoc initial</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Cant. cumparata</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Pret cumparare</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Cant. vanduta</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Pret vanzare</td>");
            builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Stoc final</td>");
            builder.AppendLine("          </tr>");

            if (elemente != null)
            {
                foreach (RaportProductieElementViewModel element in elemente)
                {
                    builder.AppendLine("          <tr>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Cod + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Produs + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.StocInitial.ToString("0.00") + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.CantitateCumparata.ToString("0.00") + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.PretCumparare.ToString("0.00") + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.CantitateVanduta.ToString("0.00") + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.PretVanzare.ToString("0.00") + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.StocFinal.ToString("0.00") + "</td>");
                    builder.AppendLine("          </tr>");
                }

                builder.AppendLine("        <tr>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("        </tr>");
            }

            builder.AppendLine("      </table>");
            builder.AppendLine("      <br />");
            builder.AppendLine("      <b>Gestionar, </b><br />");
            builder.AppendLine("      <b>Farcas Ana-Doina, </b><br />");
            builder.AppendLine("  </body>");
            builder.AppendLine("</html>");

            return builder.ToString();
        }
        public static string GenereazaRaportCentralizator(DateTime data, List<CentralizatorElementViewModel> elemente)
        {
            StringBuilder builder = new StringBuilder();
            string headerStyle = "border-left-width: 1px; border-color: #999999; font-weight: bold; border-style: solid; border-width: 1px; border-collapse: collapse; ";
            string rowStyle = "border-style: solid; border-color: #999999; border-left-width: 1px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 0px; border-collapse: collapse; ";
            string footerStyle = "border-style: solid; border-color: #999999; border-top-width: 1px; border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-collapse: collapse; ";

            builder.AppendLine("<html>");
            builder.AppendLine("  <head>");
            builder.AppendLine("      <title>Inventar</title>");
            builder.AppendLine("      <script type='text/javascript'>window.print()</script>");
            builder.AppendLine("  </head>");
            builder.AppendLine("  <body style='font-family: Calibri; font-size: 11pt; color: #999999'>");
            builder.AppendLine("  <h1 style='font-size: 16pt'>Centralizatorul de vanzari nr. ........... din data de " + data.ToString("dd/MM/yyyy") + "</h1>");
            //builder.AppendLine("  <b>Categorie: </b>" + (string.IsNullOrEmpty(categorie) == true ? "Toate" : categorie));

            if (elemente != null)
            {
                var tipuriVanzare = (from vanzare in elemente select vanzare.TipVanzare).Distinct();
                foreach (string tipVanzare in tipuriVanzare)
                {
                    builder.AppendLine("      Tip vanzare: " + tipVanzare);
                    builder.AppendLine("      <table cellpadding='2px' cellspacing='0px'>");
                    builder.AppendLine("          <tr style='text-align: center'>");
                    builder.AppendLine("              <td style='width: 70px; " + headerStyle + "'>Cod</td>");
                    builder.AppendLine("              <td style='width: 400px; " + headerStyle + "'>Produs</td>");
                    builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Portii</td>");
                    builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Pret</td>");
                    builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Valoare</td>");
                    builder.AppendLine("          </tr>");

                    var vanzariFiltrate = elemente.Where(filtru => filtru.TipVanzare == tipVanzare);
                    if (vanzariFiltrate.Count() > 0)
                    {
                        foreach (CentralizatorElementViewModel element in vanzariFiltrate)
                        {
                            builder.AppendLine("          <tr>");
                            builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Cod + "</td>");
                            builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Produs + "</td>");
                            builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Portii.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                            builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Pret.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                            builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Valoare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                            builder.AppendLine("          </tr>");
                        }

                        builder.AppendLine("        <tr>");
                        builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                        builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                        builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                        builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                        builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                        builder.AppendLine("        </tr>");
                    }

                    builder.AppendLine("      </table>");
                    builder.AppendLine("      <br />");
                }
            }

            builder.AppendLine("      <b>Gestionar, </b><br />");
            builder.AppendLine("      <b>Farcas Ana-Doina, </b><br />");
            builder.AppendLine("  </body>");
            builder.AppendLine("</html>");

            return builder.ToString();
        }
        public static string GenereazaRaportProfitModificat(ReceptieViewModel receptie)
        {
            ReteteEntities entities = new ReteteEntities();
            StringBuilder builder = new StringBuilder();
            List<PretModificatViewModel> preturiModificate = new List<PretModificatViewModel>();
            string headerStyle = "border-left-width: 1px; border-color: #999999; font-weight: bold; border-style: solid; border-width: 1px; border-collapse: collapse; ";
            string footerStyle = "border-style: solid; border-color: #999999; border-top-width: 1px; border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-collapse: collapse; ";
            string rowStyle = "border-style: solid; border-color: #999999; border-left-width: 1px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 0px; border-collapse: collapse; ";

            builder.AppendLine("<html>");
            builder.AppendLine("  <head>");
            builder.AppendLine("      <title>Inventar</title>");
            builder.AppendLine("      <script type='text/javascript'>window.print()</script>");
            builder.AppendLine("  </head>");
            builder.AppendLine("  <body style='font-family: Calibri; font-size: 11pt; color: #999999'>");
            builder.AppendLine("      <h1 style='font-size: 16pt'>Receptia " + receptie.Receptie.Numar.ToString() + " din data " + receptie.Receptie.Data.ToString("dd/MM/yyyy") + "</h1>");
            builder.AppendLine("      <b>TVA: </b>" + (receptie.Receptie.Tva * 100).ToString("0", CultureInfo.InvariantCulture) + "%" + "<br />");
            builder.AppendLine("      <b>Furnizor: </b>" + receptie.Receptie.FurnizorNume + "<br />");
            builder.AppendLine("      Receptie pentru documentul tip " + receptie.Receptie.TipDocumentLivrare + ", numar " + receptie.Receptie.NumarDocumentLivrare + "<br /><br />");

            if (receptie.Produse != null)
            {
                var categorii = from element in receptie.Produse
                                select element.Produs.Categorie;
                foreach (string categorie in categorii.Distinct().ToList())
                {
                    builder.AppendLine("      <b>Categorie: </b>" + categorie + "<br />");
                    builder.AppendLine("      <table cellpadding='2px' cellspacing='0px' style='width: 100%'>");
                    builder.AppendLine("          <tr style='text-align: center'>");
                    builder.AppendLine("              <td style='width: 80px; " + headerStyle + "'>Cod</td>");
                    builder.AppendLine("              <td style='width: 400px; " + headerStyle + "'>Produs</td>");
                    builder.AppendLine("              <td style='width: 60px; " + headerStyle + "'>UM</td>");
                    builder.AppendLine("              <td style='width: 60px; " + headerStyle + "'>Gramaj</td>");
                    builder.AppendLine("              <td style='width: 60px; " + headerStyle + "'>Cant.</td>");
                    builder.AppendLine("              <td style='width: 60px; " + headerStyle + "'>Pret</td>");
                    builder.AppendLine("              <td style='width: 80px; " + headerStyle + "'>Valoare</td>");
                    builder.AppendLine("              <td style='width: 80px; " + headerStyle + "'>Val. TVA</td>");
                    builder.AppendLine("              <td style='width: 80px; " + headerStyle + "'>Val. cu TVA</td>");
                    builder.AppendLine("              <td style='width: 100px; " + headerStyle + "'>Pret vanzare</td>");
                    builder.AppendLine("              <td style='width: 70px; " + headerStyle + "'>Adaos</td>");
                    builder.AppendLine("          </tr>");

                    foreach (ReceptieProdus element in receptie.Produse.Where(filtru => filtru.Produs.Categorie == categorie))
                    {
                        ReceptieProdus produsReceptieAnterioara = entities.ReceptiiProduse.Where(filtru => filtru.Produs.ID == element.IDProdus).OrderByDescending(filtru => filtru.Receptie.Data).Skip(1).FirstOrDefault();
                        if (produsReceptieAnterioara != null && produsReceptieAnterioara.PretVanzare != element.PretVanzare) preturiModificate.Add(new PretModificatViewModel() { IDProdus = element.IDProdus, NumeProdus = element.Produs.Nume, UnitateMasura = element.Produs.UnitateMasura, PretVechi = produsReceptieAnterioara.PretVanzare, PretNou = element.PretVanzare, CodProdus = element.Produs.Cod });

                        builder.AppendLine("          <tr>");
                        builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Produs.Cod + "</td>");
                        builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Produs.Nume + "</td>");
                        builder.AppendLine("              <td style='text-align: center; " + rowStyle + "'>" + element.Produs.UnitateMasura + "</td>");
                        builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Gramaj + "</td>");
                        builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Cantitate + "</td>");
                        builder.AppendLine("              <td style='" + rowStyle + "'>" + element.PretCumparare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                        builder.AppendLine("              <td style='" + rowStyle + "'>" + element.Valoare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                        builder.AppendLine("              <td style='" + rowStyle + "'>" + element.ValoareTVA.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                        builder.AppendLine("              <td style='" + rowStyle + "'>" + element.ValoareCuTVA.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                        builder.AppendLine("              <td style='" + rowStyle + "'>" + element.PretVanzare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                        builder.AppendLine("              <td style='" + rowStyle + "'>" + element.ProcentAdaosComercial.ToString("0", CultureInfo.InvariantCulture) + "%" + "</td>");
                        builder.AppendLine("          </tr>");
                    }

                    builder.AppendLine("        <tr>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>&nbsp;</td>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>&nbsp;</td>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>&nbsp;</td>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>&nbsp;</td>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>&nbsp;</td>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>&nbsp;</td>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>" + receptie.TotalValoare.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>" + receptie.TotalValoareTVA.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>" + receptie.TotalValoareCuTVA.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>&nbsp;</td>");
                    builder.AppendLine("            <td style='" + footerStyle + "'>&nbsp;</td>");
                    builder.AppendLine("        </tr>");

                    builder.AppendLine("      </table>");
                    builder.AppendLine("      <br />");
                }
            }

            builder.AppendLine("<b>Totaluri pe grupe de TVA</b><br />");
            builder.AppendLine("<table cellpadding='2px' cellspacing='0px'>");
            builder.AppendLine("    <tr>");
            builder.AppendLine("        <th style='" + headerStyle + "'>TVA</th>");
            builder.AppendLine("        <th style='" + headerStyle + "'>Valoare</th>");
            builder.AppendLine("        <th style='" + headerStyle + "'>Valoare cu TVA</th>");
            builder.AppendLine("    </tr>");
            builder.AppendLine("    <tr>");
            builder.AppendLine("        <td style='" + rowStyle + "'>" + receptie.Receptie.Tva.ToString("0.00") + "</th>");
            builder.AppendLine("        <td style='" + rowStyle + "'>" + receptie.Produse.Where(element => element.AdmiteTVA == true).Sum(element => element.Valoare).ToString("0.00") + "</th>");
            builder.AppendLine("        <td style='" + rowStyle + "'>" + receptie.Produse.Where(element => element.AdmiteTVA == true).Sum(element => element.ValoareCuTVA).ToString("0.00") + "</th>");
            builder.AppendLine("    </tr>");
            builder.AppendLine("    <tr>");
            builder.AppendLine("        <td style='" + footerStyle + "'>&nbsp;</th>");
            builder.AppendLine("        <td style='" + footerStyle + "'>" + receptie.Produse.Where(element => element.AdmiteTVA == false).Sum(element => element.Valoare).ToString("0.00") + "</th>");
            builder.AppendLine("        <td style='" + footerStyle + "'>" + receptie.Produse.Where(element => element.AdmiteTVA == false).Sum(element => element.ValoareCuTVA).ToString("0.00") + "</th>");
            builder.AppendLine("    </tr>");
            builder.AppendLine("</table>");
            builder.AppendLine("      <br />");

            if (preturiModificate.Count > 0)
            {
                builder.AppendLine("      <h1 style='font-size: 16pt'>Proces verbal</h1>");
                builder.AppendLine("      <b>Urmatoarele produse au pretul de vanzare modificat fata de ultima receptie</b><br />");

                builder.AppendLine("      <table cellpadding='2px' cellspacing='0px' style='width: 100%'>");
                builder.AppendLine("          <tr style='text-align: center'>");
                builder.AppendLine("              <td style='width: 80px; " + headerStyle + "'>Cod</td>");
                builder.AppendLine("              <td style='width: 400px; " + headerStyle + "'>Produs</td>");
                builder.AppendLine("              <td style='width: 60px; " + headerStyle + "'>UM</td>");
                builder.AppendLine("              <td style='width: 60px; " + headerStyle + "'>Pret anterior</td>");
                builder.AppendLine("              <td style='width: 80px; " + headerStyle + "'>Pret curent</td>");
                builder.AppendLine("          </tr>");

                foreach (PretModificatViewModel element in preturiModificate)
                {
                    builder.AppendLine("          <tr>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.CodProdus + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.NumeProdus + "</td>");
                    builder.AppendLine("              <td style='text-align: center; " + rowStyle + "'>" + element.UnitateMasura + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.PretVechi.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("              <td style='" + rowStyle + "'>" + element.PretNou.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    builder.AppendLine("          </tr>");
                }

                builder.AppendLine("        <tr>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("            <td style='" + footerStyle + "'></td>");
                builder.AppendLine("        </tr>");

                builder.AppendLine("      </table>");
                builder.AppendLine("      <br />");
            }

            builder.AppendLine("      <b>Gestionar, </b><br />");
            builder.AppendLine("      <b>Farcas Ana-Doina, </b><br />");
            builder.AppendLine("  </body>");
            builder.AppendLine("</html>");

            return builder.ToString();
        }
        public static string GenereazaRaportProduseCasate(List<ElementStoc> produseCasate)
        {
            using (StringWriter writer = new StringWriter())
            {
                writer.WriteLine("<html>");
                writer.WriteLine("  <head>");
                writer.WriteLine("      <title>Produse casate</title>");
                writer.WriteLine("      <script type='text/javascript'>window.print()</script>");
                writer.WriteLine("  </head>");
                writer.WriteLine("  <body style='font-family: Calibri; font-size: 11pt; color: #999999'>");
                writer.WriteLine("  <h1 style='font-size: 16pt'>Lista produselor casate la data " + DateTime.Today.ToString("dd/MM/yyyy") + "</h1>");
                writer.WriteLine("      <table>");
                writer.WriteLine("          <tr>");
                writer.WriteLine("              <td style='width: 100px; font-weight: bold;'>Cod</td>");
                writer.WriteLine("              <td style='width: 400px; font-weight: bold;'>Produs</td>");
                writer.WriteLine("              <td style='width: 100px; font-weight: bold;'>Cantitate</td>");
                writer.WriteLine("          </tr>");

                foreach (ElementStoc element in produseCasate)
                {
                    writer.WriteLine("          <tr>");
                    writer.WriteLine("              <td>" + element.Produs.Cod + "</td>");
                    writer.WriteLine("              <td>" + element.Produs.Nume + "</td>");
                    writer.WriteLine("              <td>" + element.Cantitate.ToString("0.00", CultureInfo.InvariantCulture) + "</td>");
                    writer.WriteLine("          </tr>");
                }

                writer.WriteLine("      </table>");
                writer.WriteLine("      <br />");

                writer.WriteLine("      <b>Gestionar, </b><br />");
                writer.WriteLine("      <b>Farcas Ana-Doina, </b><br />");
                writer.WriteLine("  </body>");
                writer.WriteLine("</html>");
                writer.Flush();

                return writer.ToString();
            }
        }

        public static string GenereazaFactura(VanzareViewModel model, List<ElementVanzareViewModel> elementeVanzare)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine("<html>");
            builder.AppendLine("    <head>");
            builder.AppendLine("        <title>Factura</title>");
            builder.AppendLine("        <script type='text/javascript'>window.print()</script>");
            builder.AppendLine("        <style type='text/css'>");
            builder.AppendLine("            table.factura { border-width: 1px; border-spacing: ; border-style: outset; border-color: #999999; border-collapse: collapse; background-color: white; font-family: Arial; font-size: 9px }");
            builder.AppendLine("            table.factura th { border-width: 1px; padding: 1px; border-style: inset; border-color: #999999; background-color: white; font-family: Arial; font-size: 9px }");
            builder.AppendLine("    	    table.factura td { border-width: 1px; padding: 1px; border-style: inset; border-color: #999999; background-color: white; font-family: Arial; font-size: 9px }");
            builder.AppendLine("		    table.chitanta { border-width: 1px; border-spacing: ; border-style: outset; border-color: #999999; border-collapse: collapse; background-color: white; font-family: Arial; font-size: 9px }");
            builder.AppendLine("	    </style>");
            builder.AppendLine("    </head>");
            builder.AppendLine("    <body style='color: #999999'>");
            builder.AppendLine("        <table width='100%' cellpadding='0' cellspacing='0' style='font-family: Arial; font-size: 9px'>");
            builder.AppendLine("            <tr>");
            builder.AppendLine("                <td style='width: 33%; vertical-align: top; padding: 3px'>");
            builder.AppendLine("                    Furnizor: " + ConfigurationManager.AppSettings["UnitateDenumire"] + "<br />");
            builder.AppendLine("                    Registrul comertului: " + ConfigurationManager.AppSettings["UnitateCodRegistruComert"] + "<br />");
            builder.AppendLine("                    Cod fiscal: " + ConfigurationManager.AppSettings["UnitateCodFiscal"] + "<br />");
            builder.AppendLine("                    Sediul: " + ConfigurationManager.AppSettings["UnitateSediu"] + "<br />");
            builder.AppendLine("                    Punct lucru: " + ConfigurationManager.AppSettings["UnitatePunctLucru"] + "<br />");
            builder.AppendLine("                    Cont: " + ConfigurationManager.AppSettings["UnitateCont"] + "<br />");
            builder.AppendLine("                    <br />");
            builder.AppendLine("                    TVA: 24%<br />");
            builder.AppendLine("                </td>");
            builder.AppendLine("                <td style='width: 33%; text-align: center; padding: 3px'>");
            builder.AppendLine("                    <h2>FACTURA</h2><br />");
            builder.AppendLine("                    Nr facturii: " + model.FacturaSerie + " " + model.FacturaNumar.ToString().PadLeft(6, '0') + "<br />");
            builder.AppendLine("                    Data facturii: " + DateTime.Today.ToString("dd/MM/yyyy") + "<br />");
            builder.AppendLine("                </td>");
            builder.AppendLine("                <td style='width: 33%; vertical-align: top; padding: 3px'>");
            builder.AppendLine("                    Cumparator: " + model.ClientNume + "<br />");
            builder.AppendLine("                    Sediul: " + model.ClientAdresa + ", " + model.ClientJudet + "<br />");
            builder.AppendLine("                    Banca: " + model.ClientBanca + "<br />");
            builder.AppendLine("                    Cont: " + model.ClientCont + "<br />");
            builder.AppendLine("                    Nr. ord. reg. com/an: " + model.ClientCodRegistruComert + "<br />");
            builder.AppendLine("                    CIF: " + model.ClientCodFiscal + "<br />");
            builder.AppendLine("                </td>");
            builder.AppendLine("            </tr>");
            builder.AppendLine("        </table>");

            builder.AppendLine("        <br />");
            builder.AppendLine();
            builder.AppendLine("        <table width='100%' height='500px' cellpadding='0' cellspacing='0' class='factura'>");
            builder.AppendLine("            <tr style='text-align: center'>");
            builder.AppendLine("                <td style='padding: 3px'>Nr.<br />crt</td>");
            builder.AppendLine("                <td colspan='2' style='padding: 3px'>Denumirea produselor sau a serviciilor</td>");
            builder.AppendLine("                <td style='padding: 3px'>U.M.</td>");
            builder.AppendLine("                <td style='padding: 3px'>Cantitatea</td>");
            builder.AppendLine("                <td style='padding: 3px'>Pretul unitar<br />(fara TVA)<br />-lei-</td>");
            builder.AppendLine("                <td style='padding: 3px'>Valoarea<br />-lei-</td>");
            builder.AppendLine("                <td style='padding: 3px'>Valoarea<br />TVA<br />-lei-</td>");
            builder.AppendLine("            </tr>");
            builder.AppendLine("            <tr style='text-align: center'>");
            builder.AppendLine("                <td style='padding: 3px'>0</td>");
            builder.AppendLine("                <td colspan='2' style='padding: 3px'>1</td>");
            builder.AppendLine("                <td style='padding: 3px'>2</td>");
            builder.AppendLine("                <td style='padding: 3px'>3</td>");
            builder.AppendLine("                <td style='padding: 3px'>4</td>");
            builder.AppendLine("                <td style='padding: 3px'>5 (3 * 4)</td>");
            builder.AppendLine("                <td style='padding: 3px'>6</td>");
            builder.AppendLine("            </tr>");

            if (model.TiparesteProdusele == true)
            {
                for (int i = 0; i < elementeVanzare.Count; i++)
                {
                    if (i > 0 && i % 35 == 0) builder.AppendLine("            <tr><td style='border: none;'><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></td></tr>");

                    builder.AppendLine("            <tr>");
                    builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>" + (i + 1).ToString() + "</td>");
                    builder.AppendLine("                <td colspan='2' style='padding: 3px; border-width: 0px 1px 0px 1px'>" + elementeVanzare[i].Nume + "</td>");
                    builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>portie</td>");
                    builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>" + elementeVanzare[i].Portii + "</td>");
                    builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>" + (elementeVanzare[i].Pret / 1.24m).ToString("0.00") + "</td>");
                    builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>" + ((elementeVanzare[i].Pret / 1.24m) * elementeVanzare[i].Portii).ToString("0.00") + "</td>");
                    builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>" + (elementeVanzare[i].Pret / 1.24m * 0.24m * elementeVanzare[i].Portii).ToString("0.00") + "</td>");
                    builder.AppendLine("            </tr>");
                }
            }
            else
            {
                builder.AppendLine("            <tr>");
                builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>1</td>");
                builder.AppendLine("                <td colspan='2' style='padding: 3px; border-width: 0px 1px 0px 1px'>Produse conform bon fiscal numar ...........</td>");
                builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>-</td>");
                builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>1</td>");
                builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>" + (elementeVanzare.Sum(element => element.Pret / 1.24m)).ToString("0.00") + "</td>");
                builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>" + (elementeVanzare.Sum(element => (element.Pret / 1.24m) * element.Portii)).ToString("0.00") + "</td>");
                builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'>" + (elementeVanzare.Sum(element => element.Pret / 1.24m * 0.24m * element.Portii)).ToString("0.00") + "</td>");
                builder.AppendLine("            </tr>");
            }

            builder.AppendLine("            <tr style='height: 100%'>");
            builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'></td>");
            builder.AppendLine("                <td colspan='2' valign='bottom' style='padding: 3px; border-width: 0px 1px 0px 1px'>");
            builder.AppendLine("                    " + ConfigurationManager.AppSettings["EmitentFacturaNume"] + "<br />");
            builder.AppendLine("                    " + ConfigurationManager.AppSettings["EmitentFacturaBuletinSerie"] + " " + ConfigurationManager.AppSettings["EmitentFacturaBuletinNumar"] + " CNP " + ConfigurationManager.AppSettings["EmitentFacturaCNP"]);
            builder.AppendLine("                </td>");
            builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'></td>");
            builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'></td>");
            builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'></td>");
            builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'></td>");
            builder.AppendLine("                <td style='text-align: center; padding: 3px; border-width: 0px 1px 0px 1px'></td>");
            builder.AppendLine("            </tr>");

            builder.AppendLine("            <tr>");
            builder.AppendLine("                <td colspan='2' rowspan='2' valign='top' style='padding: 3px'>Semnatura si<br />stampila<br />furnizorului</td>");
            builder.AppendLine("                <td colspan='3' rowspan='2' style='padding: 3px'>");
            builder.AppendLine("                    Date privind expeditia<br />");
            builder.AppendLine("                    Numele delegatului: " + ConfigurationManager.AppSettings["DelegatNume"] + "<br />");
            builder.AppendLine("                    CI Seria: " + ConfigurationManager.AppSettings["DelegatBuletinSerie"] + ", nr: " + ConfigurationManager.AppSettings["DelegatBuletinNumar"] + ", eliberat(a): " + ConfigurationManager.AppSettings["DelegatBuletinPolitie"] + "<br />");
            builder.AppendLine("                    Mijlocul de transport: " + ConfigurationManager.AppSettings["DelegatTransport"] + "<br />");
            builder.AppendLine("                    Expedierea s-a facut in prezenta noastra la data de: " + DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) + ", ora: " + DateTime.Now.ToString("hh:mm:ss", CultureInfo.InvariantCulture) + "<br />");
            builder.AppendLine("                    Semnaturile: .................................<br />");
            builder.AppendLine("                </td>");
            builder.AppendLine("                <td valign='top' style='padding: 3px'>Total</td>");
            builder.AppendLine("                <td valign='top' style='padding: 3px; text-align: center'>" + elementeVanzare.Sum(element => (element.Pret / 1.24m) * element.Portii).ToString("0.00") + "</td>");
            builder.AppendLine("                <td valign='top' style='padding: 3px; text-align: center'>" + elementeVanzare.Sum(element => (element.Pret / 1.24m * 0.24m * element.Portii)).ToString("0.00") + "</td>");
            builder.AppendLine("            </tr>");
            builder.AppendLine("            <tr>");
            builder.AppendLine("                <td style='padding: 3px'>Semnatura<br />de primire</td>");
            builder.AppendLine("                <td colspan='2' style='padding: 3px'>");
            builder.AppendLine("                    <b>");
            builder.AppendLine("                        Total de plata<br />(col. 5 + col. 6)<br />");
            builder.AppendLine("                        <div style='text-align: center'>" + elementeVanzare.Sum(element => element.Pret * element.Portii).ToString("0.00") + "</div>");
            builder.AppendLine("                    </b>");
            builder.AppendLine("                </td>");
            builder.AppendLine("            </tr>");
            builder.AppendLine("        </table>");
            builder.AppendLine("    </body>");
            builder.AppendLine("</html>");

            return builder.ToString();
        }
        public static string GenereazaReteta(int idReteta)
        {
            StringBuilder builder = new StringBuilder();
            ReteteEntities entities = new ReteteEntities();
            Reteta model = entities.Retete.First(element => element.ID == idReteta);
            
            builder.AppendLine("<html>");
            builder.AppendLine("    <head>");
            builder.AppendLine("        <title>Reteta</title>");
            builder.AppendLine("        <script type='text/javascript'>window.print()</script>");
            builder.AppendLine("    </head>");
            builder.AppendLine("    <body style='color: #999999'>");
            builder.AppendLine("        <span style='font-family: Arial; font-size: 18px'>" + model.Nume + "</span>");

            builder.AppendLine("        <table cellpadding='0' cellspacing='0' style='font-family: Arial; font-size: 16px'>");
            builder.AppendLine("            <tr>");
            builder.AppendLine("                <td style='width: 300px'>Produs</td>");
            builder.AppendLine("                <td>Cantitate</td>");
            builder.AppendLine("            </tr>");

            foreach (RetetaProdus element in model.Produse)
            {
                builder.AppendLine("            <tr>");
                builder.AppendLine("                <td style='width: 300px'>" + element.Produs.Nume + "</td>");
                builder.AppendLine("                <td>" + element.Cantitate + "</td>");
                builder.AppendLine("            </tr>");
            }
            builder.AppendLine("        </table>");

            builder.AppendLine("    </body>");
            builder.AppendLine("</html>");

            return builder.ToString();
        }

        public static List<StocViewModel> GenereazaStoc(DateTime data)
        {
            ReteteEntities entities = new ReteteEntities();
            ElementStoc ultimulElement = entities.Stoc.Where(element => element.Data <= data).OrderByDescending(element => element.Data).FirstOrDefault();
            List<StocViewModel> stoc = new List<StocViewModel>();

            // aduc ultimele cantitati sau zero daca nu am asa ceva
            foreach (Produs element in entities.Produse) stoc.Add(new StocViewModel() { ID = element.ID, Nume = element.Nume, Cod = element.Cod, Cantitate = 0, Pret = element.Pret, PretCumparare = element.PretCumparare, UnitateMasura = element.UnitateMasura, Categorie = element.Categorie });
            if (ultimulElement != null)
            {
                foreach (ElementStoc element in entities.Stoc.Where(filtru => filtru.Data == ultimulElement.Data))
                {
                    StocViewModel selectie = stoc.FirstOrDefault(filtru => filtru.ID == element.IDProdus);
                    if (selectie != null) selectie.Cantitate = element.Cantitate;
                }
            }

            // adun receptiile si iau ultimul pret de cumparare
            if (ultimulElement == null)
            {
                foreach (StocViewModel element in stoc)
                {
                    List<ReceptieProdus> receptiiProdusSelectat = entities.ReceptiiProduse.Where(filtru => filtru.IDProdus == element.ID && filtru.Receptie.Data <= data).OrderByDescending(filtru => filtru.Receptie.Data).ToList();

                    element.Cantitate += receptiiProdusSelectat.Sum(filtru => filtru.Cantitate * filtru.Gramaj);
                    if (receptiiProdusSelectat.Count > 0)
                    {
                        element.PretCumparare = receptiiProdusSelectat[0].PretCumparare / receptiiProdusSelectat[0].Gramaj;
                        element.Pret = receptiiProdusSelectat[0].PretVanzare;
                    }
                }
            }
            else
            {
                foreach (StocViewModel element in stoc)
                {
                    List<ReceptieProdus> receptiiProdusSelectat = entities.ReceptiiProduse.Where(filtru => filtru.IDProdus == element.ID && filtru.Receptie.Data > ultimulElement.Data && filtru.Receptie.Data <= data).OrderByDescending(filtru => filtru.Receptie.Data).ToList();

                    element.Cantitate += receptiiProdusSelectat.Sum(filtru => filtru.Cantitate * filtru.Gramaj);
                    if (receptiiProdusSelectat.Count > 0)
                    {
                        element.PretCumparare = receptiiProdusSelectat[0].PretCumparare / receptiiProdusSelectat[0].Gramaj;
                        element.Pret = receptiiProdusSelectat[0].PretVanzare;
                    }
                }
            }

            // scad vanzarile
            if (ultimulElement == null)
            {
                var vanzari = from elementeVanzari in entities.ElementeVanzari
                              join reteteProduse in entities.ReteteProduse on elementeVanzari.IDReteta equals reteteProduse.IDReteta into vanzariReteteProduse
                              from reteteProduse in vanzariReteteProduse.DefaultIfEmpty()
                              join meniuriRetete in entities.MeniuriRetete on elementeVanzari.IDMeniu equals meniuriRetete.IDMeniu into vanzariMeniuriRetete
                              from meniuriRetete in vanzariMeniuriRetete.DefaultIfEmpty()
                              join meniuriReteteProduse in entities.ReteteProduse on meniuriRetete.IDReteta equals meniuriReteteProduse.IDReteta into vanzariMeniuriReteteProduse
                              from meniuriReteteProduse in vanzariMeniuriReteteProduse.DefaultIfEmpty()
                              where elementeVanzari.Vanzare.Data <= data
                              select new
                              {
                                  IDVanzare = elementeVanzari.IDVanzare,
                                  IDProdus = ((int?)elementeVanzari.IDProdus ?? (int?)reteteProduse.IDProdus) ?? (int?)meniuriReteteProduse.IDProdus,
                                  Cantitate =
                                  elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Portii :
                                  elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Portii / elementeVanzari.Reteta.MultiplicatorCantitati * reteteProduse.Cantitate :
                                  elementeVanzari.IDMeniu.HasValue == true ? elementeVanzari.Portii * meniuriReteteProduse.Cantitate * meniuriRetete.Portii / meniuriRetete.Reteta.MultiplicatorCantitati : 0
                              };
                var vanzariGrupate = from vanzare in vanzari
                                     group vanzare by vanzare.IDProdus into grupe
                                     select new { IDProdus = grupe.Key, Cantitate = grupe.Sum(element => element.Cantitate) };
                var listaVanzariGrupate = vanzariGrupate.ToList();

                foreach (StocViewModel element in stoc)
                {
                    var selectie = listaVanzariGrupate.FirstOrDefault(filtru => filtru.IDProdus == element.ID);
                    if (selectie != null) element.Cantitate -= selectie.Cantitate;
                }
            }
            else
            {
                var vanzari = from elementeVanzari in entities.ElementeVanzari
                              join reteteProduse in entities.ReteteProduse on elementeVanzari.IDReteta equals reteteProduse.IDReteta into vanzariReteteProduse
                              from reteteProduse in vanzariReteteProduse.DefaultIfEmpty()
                              join meniuriRetete in entities.MeniuriRetete on elementeVanzari.IDMeniu equals meniuriRetete.IDMeniu into vanzariMeniuriRetete
                              from meniuriRetete in vanzariMeniuriRetete.DefaultIfEmpty()
                              join meniuriReteteProduse in entities.ReteteProduse on meniuriRetete.IDReteta equals meniuriReteteProduse.IDReteta into vanzariMeniuriReteteProduse
                              from meniuriReteteProduse in vanzariMeniuriReteteProduse.DefaultIfEmpty()
                              where elementeVanzari.Vanzare.Data > ultimulElement.Data && elementeVanzari.Vanzare.Data <= data
                              select new
                              {
                                  IDVanzare = elementeVanzari.IDVanzare,
                                  IDProdus = ((int?)elementeVanzari.IDProdus ?? (int?)reteteProduse.IDProdus) ?? (int?)meniuriReteteProduse.IDProdus,
                                  Cantitate =
                                  elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Portii :
                                  elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Portii / elementeVanzari.Reteta.MultiplicatorCantitati * reteteProduse.Cantitate :
                                  elementeVanzari.IDMeniu.HasValue == true ? elementeVanzari.Portii * meniuriReteteProduse.Cantitate * meniuriRetete.Portii / meniuriRetete.Reteta.MultiplicatorCantitati : 0
                              };
                var vanzariGrupate = from vanzare in vanzari
                                     group vanzare by vanzare.IDProdus into grupe
                                     select new { IDProdus = grupe.Key, Cantitate = grupe.Sum(element => element.Cantitate) };
                var listaVanzariGrupate = vanzariGrupate.ToList();

                foreach (StocViewModel element in stoc)
                {
                    var selectie = listaVanzariGrupate.FirstOrDefault(filtru => filtru.IDProdus == element.ID);
                    if (selectie != null) element.Cantitate -= selectie.Cantitate;
                }
            }

            foreach (StocViewModel element in stoc) element.CantitateTeoretica = element.Cantitate;
            return stoc.OrderBy(element => element.Nume).ToList();
        }
        public static List<StocViewModel> GenereazaVanzari(DateTime data, string categorie)
        {
            ReteteEntities entities = new ReteteEntities();

            var vanzari = from elementeVanzari in entities.ElementeVanzari
                          join reteteProduse in entities.ReteteProduse on elementeVanzari.IDReteta equals reteteProduse.IDReteta into vanzariReteteProduse
                          from reteteProduse in vanzariReteteProduse.DefaultIfEmpty()
                          join meniuriRetete in entities.MeniuriRetete on elementeVanzari.IDMeniu equals meniuriRetete.IDMeniu into vanzariMeniuriRetete
                          from meniuriRetete in vanzariMeniuriRetete.DefaultIfEmpty()
                          join meniuriReteteProduse in entities.ReteteProduse on meniuriRetete.IDReteta equals meniuriReteteProduse.IDReteta into vanzariMeniuriReteteProduse
                          from meniuriReteteProduse in vanzariMeniuriReteteProduse.DefaultIfEmpty()
                          where elementeVanzari.Vanzare.Data == data
                          && (elementeVanzari.Vanzare.Tip == "Plata numerar" || elementeVanzari.Vanzare.Tip == "Plata prin virament")
                          && (string.IsNullOrEmpty(categorie) || (reteteProduse != null ? reteteProduse.Produs.Categorie.Contains(categorie) :
                          meniuriReteteProduse != null ? meniuriReteteProduse.Produs.Categorie.Contains(categorie) :
                          elementeVanzari.Produs != null ? elementeVanzari.Produs.Categorie.Contains(categorie) : false))
                          select new
                          {
                              IDVanzare = elementeVanzari.IDVanzare,
                              IDProdus = ((int?)elementeVanzari.IDProdus ?? (int?)reteteProduse.IDProdus) ?? (int?)meniuriReteteProduse.IDProdus,
                              Cod =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Produs.Cod :
                              elementeVanzari.IDReteta.HasValue == true ? reteteProduse.Produs.Cod :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriReteteProduse.Produs.Cod : string.Empty,
                              UnitateMasura =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Produs.UnitateMasura :
                              elementeVanzari.IDReteta.HasValue == true ? reteteProduse.Produs.UnitateMasura :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriReteteProduse.Produs.UnitateMasura : string.Empty,
                              Categorie =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Produs.Categorie :
                              elementeVanzari.IDReteta.HasValue == true ? reteteProduse.Produs.Categorie :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriReteteProduse.Produs.Categorie : string.Empty,
                              Cantitate =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Portii :
                              elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Portii / elementeVanzari.Reteta.MultiplicatorCantitati * reteteProduse.Cantitate :
                              elementeVanzari.IDMeniu.HasValue == true ? elementeVanzari.Portii * meniuriReteteProduse.Cantitate * meniuriRetete.Portii / meniuriRetete.Reteta.MultiplicatorCantitati : 0,
                              Produs =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Produs.Nume :
                              elementeVanzari.IDReteta.HasValue == true ? reteteProduse.Produs.Nume :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriReteteProduse.Produs.Nume : string.Empty,
                              Pret =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Produs.PretCumparare :
                              elementeVanzari.IDReteta.HasValue == true ? reteteProduse.Produs.PretCumparare :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriReteteProduse.Produs.PretCumparare : 0
                          };
            var vanzariGrupate = from vanzare in vanzari
                                 group vanzare by new { IDProdus = vanzare.IDProdus, Produs = vanzare.Produs, Cod = vanzare.Cod, UnitateMasura = vanzare.UnitateMasura, Categorie = vanzare.Categorie } into grupe
                                 select new StocViewModel() { ID = grupe.Key.IDProdus.Value, Nume = grupe.Key.Produs, Cod = grupe.Key.Cod, Categorie = grupe.Key.Categorie, UnitateMasura = grupe.Key.UnitateMasura, Cantitate = grupe.Sum(element => element.Cantitate), Pret = grupe.Average(element => element.Pret), PretCumparare = grupe.Average(element => element.Pret) };

            List<StocViewModel> rezultat = vanzariGrupate.ToList();

            // obtin ultimele preturi de achizitie pentru produse
            ElementStoc ultimulElement = entities.Stoc.Where(element => element.Data <= data).OrderByDescending(element => element.Data).FirstOrDefault();
            if (ultimulElement == null)
            {
                foreach (StocViewModel element in rezultat)
                {
                    List<ReceptieProdus> receptiiProdusSelectat = entities.ReceptiiProduse.Where(filtru => filtru.IDProdus == element.ID && filtru.Receptie.Data <= data).OrderByDescending(filtru => filtru.Receptie.Data).ToList();
                    if (receptiiProdusSelectat.Count > 0)
                    {
                        element.PretCumparare = receptiiProdusSelectat[0].PretCumparare / receptiiProdusSelectat[0].Gramaj;
                        element.Pret = receptiiProdusSelectat[0].PretVanzare;
                    }
                }
            }
            else
            {
                foreach (StocViewModel element in rezultat)
                {
                    List<ReceptieProdus> receptiiProdusSelectat = entities.ReceptiiProduse.Where(filtru => filtru.IDProdus == element.ID && filtru.Receptie.Data > ultimulElement.Data && filtru.Receptie.Data <= data).OrderByDescending(filtru => filtru.Receptie.Data).ToList();
                    if (receptiiProdusSelectat.Count > 0)
                    {
                        element.PretCumparare = receptiiProdusSelectat[0].PretCumparare / receptiiProdusSelectat[0].Gramaj;
                        element.Pret = receptiiProdusSelectat[0].PretVanzare;
                    }
                }
            }

            return rezultat;
        }
        public static List<StocViewModel> GenereazaReceptii(DateTime data)
        {
            ReteteEntities entities = new ReteteEntities();

            var receptii = from receptie in entities.ReceptiiProduse
                           where receptie.Receptie.Data == data
                           group receptie by receptie.IDProdus into grupe
                           select new StocViewModel() { ID = grupe.Key, Cantitate = grupe.Sum(element => element.Cantitate * element.Gramaj) };

            return receptii.ToList();
        }
        public static List<StocViewModel> GenereazaVanzariRetete(DateTime data)
        {
            ReteteEntities entities = new ReteteEntities();

            var vanzari = from elementeVanzari in entities.ElementeVanzari
                          join meniuriRetete in entities.MeniuriRetete on elementeVanzari.IDMeniu equals meniuriRetete.IDMeniu into vanzariMeniuriRetete
                          from meniuriRetete in vanzariMeniuriRetete.DefaultIfEmpty()
                          where elementeVanzari.Vanzare.Data == data && (elementeVanzari.Vanzare.Tip == "Plata numerar" || elementeVanzari.Vanzare.Tip == "Plata prin virament")
                          select new
                          {
                              IDVanzare = elementeVanzari.IDVanzare,
                              IDReteta = (int?)elementeVanzari.IDReteta ?? (int?)meniuriRetete.IDReteta,
                              UnitateMasura = "buc",
                              Categorie =
                              elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Reteta.Categorie :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriRetete.Reteta.Categorie : string.Empty,
                              Cantitate =
                              elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Portii :
                              elementeVanzari.IDMeniu.HasValue == true ? elementeVanzari.Portii * meniuriRetete.Portii : 0,
                              Cod = elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Reteta.Cod :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriRetete.Reteta.Cod : string.Empty,
                              Reteta =
                              elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Reteta.Nume :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriRetete.Reteta.Nume : string.Empty,
                              Pret =
                              elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Reteta.Pret :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriRetete.Reteta.Pret : 0
                          };
            var vanzariGrupate = from vanzare in vanzari
                                 group vanzare by new { IDProdus = vanzare.IDReteta, Produs = vanzare.Reteta, Cod = vanzare.Cod, UnitateMasura = vanzare.UnitateMasura, Categorie = vanzare.Categorie } into grupe
                                 select new StocViewModel() { ID = grupe.Key.IDProdus.Value, Nume = grupe.Key.Produs, Cod = grupe.Key.Cod, Categorie = grupe.Key.Categorie, UnitateMasura = grupe.Key.UnitateMasura, Cantitate = grupe.Sum(element => element.Cantitate), Pret = grupe.Average(element => element.Pret), PretCumparare = grupe.Average(element => element.Pret) };

            List<StocViewModel> rezultat = vanzariGrupate.ToList();
            return rezultat;
        }
        public static List<StocViewModel> GenereazaVanzariSimple(DateTime data)
        {
            ReteteEntities entities = new ReteteEntities();

            var vanzari = from elementeVanzari in entities.ElementeVanzari
                          where elementeVanzari.Vanzare.Data == data && (elementeVanzari.Vanzare.Tip == "Plata numerar" || elementeVanzari.Vanzare.Tip == "Plata prin virament")
                          select new
                          {
                              IDVanzare = elementeVanzari.IDVanzare,
                              IDElementVanzare = 
                              elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Reteta.ID :
                              elementeVanzari.IDMeniu.HasValue == true ? elementeVanzari.Meniu.ID : 0,
                              UnitateMasura = "buc",
                              Categorie =
                              elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Reteta.Categorie :
                              elementeVanzari.IDMeniu.HasValue == true ? "Meniu" : string.Empty,
                              Cantitate = elementeVanzari.Portii,
                              Cod = 
                              elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Reteta.Cod :
                              elementeVanzari.IDMeniu.HasValue == true ? elementeVanzari.Meniu.Cod : string.Empty,
                              Nume =
                              elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Reteta.Nume :
                              elementeVanzari.IDMeniu.HasValue == true ? elementeVanzari.Meniu.Denumire : string.Empty,
                              Pret = elementeVanzari.Pret
                          };
            var vanzariGrupate = from vanzare in vanzari
                                 group vanzare by new { IDElementVanzare = vanzare.IDElementVanzare, Nume = vanzare.Nume, Cod = vanzare.Cod, UnitateMasura = vanzare.UnitateMasura, Categorie = vanzare.Categorie } into grupe
                                 select new StocViewModel() { ID = grupe.Key.IDElementVanzare, Nume = grupe.Key.Nume, Cod = grupe.Key.Cod, Categorie = grupe.Key.Categorie, UnitateMasura = grupe.Key.UnitateMasura, Cantitate = grupe.Sum(element => element.Cantitate), Pret = grupe.Average(element => element.Pret), PretCumparare = grupe.Average(element => element.Pret) };

            List<StocViewModel> rezultat = vanzariGrupate.ToList();
            return rezultat;
        }

        public static List<StocViewModel> GenereazaVanzariLunare(int luna, int an, string categorie)
        {
            ReteteEntities entities = new ReteteEntities();

            var vanzari = from elementeVanzari in entities.ElementeVanzari
                          join reteteProduse in entities.ReteteProduse on elementeVanzari.IDReteta equals reteteProduse.IDReteta into vanzariReteteProduse
                          from reteteProduse in vanzariReteteProduse.DefaultIfEmpty()
                          join meniuriRetete in entities.MeniuriRetete on elementeVanzari.IDMeniu equals meniuriRetete.IDMeniu into vanzariMeniuriRetete
                          from meniuriRetete in vanzariMeniuriRetete.DefaultIfEmpty()
                          join meniuriReteteProduse in entities.ReteteProduse on meniuriRetete.IDReteta equals meniuriReteteProduse.IDReteta into vanzariMeniuriReteteProduse
                          from meniuriReteteProduse in vanzariMeniuriReteteProduse.DefaultIfEmpty()
                          where elementeVanzari.Vanzare.Data.Value.Month == luna && elementeVanzari.Vanzare.Data.Value.Year == an
                          && (elementeVanzari.Vanzare.Tip == "Plata numerar" || elementeVanzari.Vanzare.Tip == "Plata prin virament")
                          && (string.IsNullOrEmpty(categorie) || (reteteProduse != null ? reteteProduse.Produs.Categorie.Contains(categorie) :
                          meniuriReteteProduse != null ? meniuriReteteProduse.Produs.Categorie.Contains(categorie) :
                          elementeVanzari.Produs != null ? elementeVanzari.Produs.Categorie.Contains(categorie) : false))
                          select new
                          {
                              IDVanzare = elementeVanzari.IDVanzare,
                              IDProdus = ((int?)elementeVanzari.IDProdus ?? (int?)reteteProduse.IDProdus) ?? (int?)meniuriReteteProduse.IDProdus,
                              Cod =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Produs.Cod :
                              elementeVanzari.IDReteta.HasValue == true ? reteteProduse.Produs.Cod :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriReteteProduse.Produs.Cod : string.Empty,
                              UnitateMasura =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Produs.UnitateMasura :
                              elementeVanzari.IDReteta.HasValue == true ? reteteProduse.Produs.UnitateMasura :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriReteteProduse.Produs.UnitateMasura : string.Empty,
                              Categorie =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Produs.Categorie :
                              elementeVanzari.IDReteta.HasValue == true ? reteteProduse.Produs.Categorie :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriReteteProduse.Produs.Categorie : string.Empty,
                              Cantitate =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Portii :
                              elementeVanzari.IDReteta.HasValue == true ? elementeVanzari.Portii / elementeVanzari.Reteta.MultiplicatorCantitati * reteteProduse.Cantitate :
                              elementeVanzari.IDMeniu.HasValue == true ? elementeVanzari.Portii * meniuriReteteProduse.Cantitate * meniuriRetete.Portii / meniuriRetete.Reteta.MultiplicatorCantitati : 0,
                              Produs =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Produs.Nume :
                              elementeVanzari.IDReteta.HasValue == true ? reteteProduse.Produs.Nume :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriReteteProduse.Produs.Nume : string.Empty,
                              Pret =
                              elementeVanzari.IDProdus.HasValue == true ? elementeVanzari.Produs.PretCumparare :
                              elementeVanzari.IDReteta.HasValue == true ? reteteProduse.Produs.PretCumparare :
                              elementeVanzari.IDMeniu.HasValue == true ? meniuriReteteProduse.Produs.PretCumparare : 0
                          };
            var vanzariGrupate = from vanzare in vanzari
                                 group vanzare by new { IDProdus = vanzare.IDProdus, Produs = vanzare.Produs, Cod = vanzare.Cod, UnitateMasura = vanzare.UnitateMasura, Categorie = vanzare.Categorie } into grupe
                                 select new StocViewModel() { ID = grupe.Key.IDProdus.Value, Nume = grupe.Key.Produs, Cod = grupe.Key.Cod, Categorie = grupe.Key.Categorie, UnitateMasura = grupe.Key.UnitateMasura, Cantitate = grupe.Sum(element => element.Cantitate), Pret = grupe.Average(element => element.Pret), PretCumparare = grupe.Average(element => element.Pret) };

            List<StocViewModel> rezultat = vanzariGrupate.ToList();

            // obtin ultimele preturi de achizitie pentru produse
            DateTime ultimaZi = new DateTime(an, luna, new DateTime(an, luna, 1).AddMonths(1).AddDays(-1).Day);
            ElementStoc ultimulElement = entities.Stoc.Where(element => element.Data <= ultimaZi).OrderByDescending(element => element.Data).FirstOrDefault();
            if (ultimulElement == null)
            {
                foreach (StocViewModel element in rezultat)
                {
                    List<ReceptieProdus> receptiiProdusSelectat = entities.ReceptiiProduse.Where(filtru => filtru.IDProdus == element.ID && filtru.Receptie.Data <= ultimaZi).OrderByDescending(filtru => filtru.Receptie.Data).ToList();
                    if (receptiiProdusSelectat.Count > 0)
                    {
                        element.PretCumparare = receptiiProdusSelectat[0].PretCumparare / receptiiProdusSelectat[0].Gramaj;
                        element.Pret = receptiiProdusSelectat[0].PretVanzare;
                    }
                }
            }
            else
            {
                foreach (StocViewModel element in rezultat)
                {
                    List<ReceptieProdus> receptiiProdusSelectat = entities.ReceptiiProduse.Where(filtru => filtru.IDProdus == element.ID && filtru.Receptie.Data > ultimulElement.Data && filtru.Receptie.Data <= ultimaZi).OrderByDescending(filtru => filtru.Receptie.Data).ToList();
                    if (receptiiProdusSelectat.Count > 0)
                    {
                        element.PretCumparare = receptiiProdusSelectat[0].PretCumparare / receptiiProdusSelectat[0].Gramaj;
                        element.Pret = receptiiProdusSelectat[0].PretVanzare;
                    }
                }
            }

            return rezultat;
        }
        public static List<StocViewModel> GenereazaReceptiiLunare(int luna, int an)
        {
            ReteteEntities entities = new ReteteEntities();

            var receptii = from receptie in entities.ReceptiiProduse
                           where receptie.Receptie.Data.Month == luna && receptie.Receptie.Data.Year == an
                           group receptie by receptie.IDProdus into grupe
                           select new StocViewModel() { ID = grupe.Key, Cantitate = grupe.Sum(element => element.Cantitate * element.Gramaj) };

            return receptii.ToList();
        }
    }
}